var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var user = require('../../services/user.js');
var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    px: ['综合','帮助人数','好评优先'], //排序列表内容
    xw: ['不限', '金融', '咨询','互联网','建筑','4A广告','快速消费品','房地产','医疗器械','其他'], //企业划分列表内容
    qyopen: false, //点击筛选滑动弹窗显示效果，默认不显示
    qyshow: true, //用户点击闭关区域的弹窗设置，默认不显示
    pxopen: false, //排序筛选弹窗
    nzopen: false,//企业划分筛选弹窗
    nzshow: true,
    pxshow: true,
    isfull: false,
    shownavindex: '',
    pxIndex: 0, //排序内容下拉框，默认第一个
    xwIndex: 0, //企业划分内容下拉框，默认第一个
    xwSelect:'',
    pxSelect:'',
    toturInfo :[],//查询出来的导师信息
    sortword: 'totalScore', //默认排序
    pageNumber: 1
  },

 
  // 企业划分下拉框是否隐藏
  listxw: function (e) {
    if (this.data.nzopen) {
      this.setData({
        qyopen: false,
        nzopen: false,
        pxopen: false,
        nzshow: true,
        pxshow: true,
        qyshow: true,
        isfull: false,
        shownavindex: 0
      })
    } else {
      this.setData({
        nzopen: true,
        pxopen: false,
        qyopen: false,
        nzshow: false,
        pxshow: true,
        qyshow: true,
        isfull: true,
        shownavindex: e.currentTarget.dataset.nav,
        xwSelect: e.currentTarget.dataset.nav
      })
    }
  },
  // 排序下拉框是否隐藏
  listpx: function (e) {
    if (this.data.pxopen) {
      this.setData({
        qyopen: false,
        nzopen: false,
        pxopen: false,
        nzshow: true,
        pxshow: true,
        qyshow: true,
        isfull: false,
        shownavindex: 0,
      })
    } else {
      this.setData({
        nzopen: false,
        pxopen: true,
        qyopen: false,
        nzshow: true,
        pxshow: false,
        qyshow: true,
        isfull: true,
        shownavindex: e.currentTarget.dataset.nav,
        pxSelect: e.currentTarget.dataset.nav
      })
    }
    console.log(e.target)
  },

  // 点击灰色背景隐藏所有的筛选内容
  hidebg: function (e) {
    this.setData({
      qyopen: false,
      nzopen: false,
      pxopen: false,
      nzshow: true,
      pxshow: true,
      qyshow: true,
      isfull: false,
      shownavindex: 0,
    })
  },

  // 企业划分内容下拉框筛选
  selectXW: function (e) {
    console.log('企业划分内容下拉框筛选的内容是' + e.currentTarget.dataset.index);
    var xwId = '';
    if (e.currentTarget.dataset.index != 0) {
      xwId = e.currentTarget.dataset.index;
    }
    var that = this;
    let searchCondition =
    {
      pageSize: 10,
      pageNumber: 1,
      conditionList: [],
      sort: { field: this.data.sortword}
    };
    let condition1 = {
      operator: 1,
      code: 'enterpriseType',
      value: parseInt(xwId),
      type: 'Number'
    };
    let condition2 = {
      operator: 1,
      code: 'itemId',
      value: 5,
      type: 'Number'
    };
    searchCondition.conditionList.push(condition1)
    searchCondition.conditionList.push(condition2)
    this.getUrl(searchCondition);
    this.setData({
      xwIndex: e.currentTarget.dataset.index,
      xwSelect: e.currentTarget.dataset.index,
      qyopen: false,
      nzopen: false,
      pxopen: false,
      nzshow: true,
      pxshow: true,
      qyshow: true,
      isfull: false,
      shownavindex: 0,
    })
  },

  // 排序内容下拉框筛选
  selectPX: function (e) {
    console.log('排序内容下拉框筛选的内容是' + e.currentTarget.dataset.index);
    var sortWord = '';
    if (e.currentTarget.dataset.index == 1){
      sortWord = 'helpsNum'
    } else if (e.currentTarget.dataset.index == 2){
      sortWord = 'totalScore'
    }else{
      sortWord = ''
    }
    var that = this;
    that.setData({
      sortword: sortWord
    })
    var xwselect = this.data.xwSelect;
    let searchCondition =
    {
      pageSize: 10,
      pageNumber: 1,
      conditionList: [],
      sort: {
        field: sortWord
      }
    };
    let condition1 = {
      operator: 1,
      code: 'enterpriseType',
      value: parseInt(xwselect),
      type: 'Number'
    };
    let condition2 = {
      operator: 1,
      code: 'itemId',
      value: 5,
      type: 'Number'
    };
    searchCondition.conditionList.push(condition1)
    searchCondition.conditionList.push(condition2)
    this.getUrl(searchCondition);
    this.setData({
      pxIndex: e.currentTarget.dataset.index,
      qyopen: false,
      nzopen: false,
      pxopen: false,
      nzshow: true,
      pxshow: true,
      qyshow: true,
      isfull: false,
      shownavindex: 0,
    })
  },

  //根据接口获取数据
  getUrl:function(e){
    var that = this;
    var searchCondition = e;
    console.log(searchCondition);
    util.request(api.SelectToturInfo, searchCondition, 'POST', true).then(function (res) {
      console.log(res.result.list);
      if (res.code === 200) {
        //设置介绍文字
        res.result.list.forEach((value, index, arr) => {
          let userEducationExperiencesTXT = "";
          for (let i in value.userEducationExperiences) {
            if (i == value.userEducationExperiences.length - 1) {
              userEducationExperiencesTXT += value.userEducationExperiences[i].college
            } else {
              userEducationExperiencesTXT += value.userEducationExperiences[i].college + " | "
            }
          }
          value.userEducationExperiencesTXT = userEducationExperiencesTXT.substring(0, userEducationExperiencesTXT.length)
          userEducationExperiencesTXT = "";
        })
        that.setData({
          toturInfo: res.result.list
        })
      }
    })
  },

  /* 跳转导师页面 */
  goToTeacher: function (e) {
    if (e.currentTarget.dataset.id != undefined) {
      wx.navigateTo({
        url: '../../myPages/teacher/teacher?teacherId=' + e.currentTarget.dataset.id
      })
    }
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '校招实习' //页面标题
    });
    //初始加载导师信息
    let searchCondition =
    {
      pageSize: 10,
      pageNumber: 1,
      conditionList: [],
      sort: {}
    };
    let condition = {
      operator: 1,
      code: 'itemId',
      value: 5,
      type: 'Number'
    };
    searchCondition.conditionList.push(condition)
    this.getUrl(searchCondition);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var pageNum = this.data.pageNumber + 1;
    that.setData({
      pageNumber: pageNum
    })
    let searchCondition =
    {
      pageSize: 10,
      pageNumber: pageNum,
      conditionList: [],
      sort: {
        field: this.data.sortword
      }
    };
    let condition1 = {
      operator: 1,
      code: 'enterpriseType',
      value: parseInt(this.data.xwSelect),
      type: 'Number'
    };
    let condition2 = {
      operator: 1,
      code: 'itemId',
      value: 5,     //1-国内硕博 2-海外留学 3-语言考试 4-报考大学 5-校招实习
      type: 'Number'
    };
    searchCondition.conditionList.push(condition1)
    searchCondition.conditionList.push(condition2)
    console.log(searchCondition)
    util.request(api.SelectToturInfo, searchCondition, 'POST', true).then(function (res) {
      console.log(res.result.list);
      if (res.code === 200) {
        //设置介绍文字
        res.result.list.forEach((value, index, arr) => {
          let userEducationExperiencesTXT = "";
          for (let i in value.userEducationExperiences) {
            if (i == value.userEducationExperiences.length - 1) {
              userEducationExperiencesTXT += value.userEducationExperiences[i].college
            } else {
              userEducationExperiencesTXT += value.userEducationExperiences[i].college + " | "
            }
          }
          value.userEducationExperiencesTXT = userEducationExperiencesTXT.substring(0, userEducationExperiencesTXT.length)
          userEducationExperiencesTXT = "";
        })
        let newArr = that.data.toturInfo.concat(res.result.list)
        that.setData({
          toturInfo: newArr
        })
      }
    })
  },
})